package cn.xiaoxiang.basic.util;


import cn.xiaoxiang.basic.config.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件工具类
 *
 * @author huang.Mr
 * @version 1.8
 */
@Slf4j
public class MailUtil {


    public static void getMaiUtil(String Sender, String qq, String path) {
        ApplicationContext applicationContext = SpringUtil.getApplicationContext();  // 获得容器对象
        JavaMailSender bean = applicationContext.getBean(JavaMailSender.class);  // 获得对象，通过反射获得spring管理的对象

        try {
            MimeMessage mimeMessage = bean.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom(Sender);
            helper.setSubject("宠物乐园中心");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText(path, true);
            // 4.添加附件
//            helper.addAttachment("好东西.jpg",new File("C:\\Users\\86157\\Pictures\\学习资料\\117645480_758957951569900_7699270236926985110_n.jpg"));
            // 5.设置收件人
            helper.setTo(qq);
            // 6.发送邮件
            bean.send(mimeMessage);
        } catch (MessagingException e) {
            log.info("发送失败，请重新发送", e);
//            e.printStackTrace();

        }
    }
}